package com.joolun.web.controller.mall;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.joolun.common.core.controller.BaseController;
import com.joolun.common.core.domain.AjaxResult;
import com.joolun.mall.entity.WorkClick;
import com.joolun.mall.service.WorkClickService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 工作信息点击记录Controller
 *
 * @author www.joolun.com
 * @date 2023-03-17
 */
/**
 * 实习
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/workClick")
@Api(value = "workClick", tags = "实习投递记录管理")
public class WorkClickController extends BaseController {

    private final WorkClickService baseService;

    /**
     * 统计查询
     * @param id workId
     * @return
     */
    @ApiOperation(value = "统计实习工作投递人数")
    @GetMapping("/countByWorkId")
    public AjaxResult countByWorkId(@PathVariable("id") Integer id) {
        List<WorkClick> list = baseService.list(new QueryWrapper<WorkClick>().eq("work_id", id));
        return AjaxResult.success(list.size());
    }

    /**
     * 新增实习投递记录
     * @param entity 实习投递记录
     * @return R
     */
    @ApiOperation(value = "新增实习投递记录-相同记录不会新增")
    @PostMapping
    public AjaxResult save(@RequestBody WorkClick entity){
        List<WorkClick> list = baseService.list(new QueryWrapper<WorkClick>().eq("word_id", entity.getWorkId()).eq("wx_user_id", entity.getWxUserId()));
        if(list.isEmpty()){
            entity.setCreateTime(LocalDateTime.now());
            return AjaxResult.success(baseService.save(entity));
        }else{
            return AjaxResult.success();
        }
    }


}
